package com.aplink.generic.pojo.formdesign;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Locale;

import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.text.TextUtils;

import com.aplink.generic.controller.GenericApplication;

public class SelectWhere extends ArrayList<Column> implements Serializable {
    private static final long serialVersionUID = 6037436530253459416L;
    private PassingValue mPassingValue;
    private String mTargetClass;

    public SelectWhere() {

    }

    private ActivityInfo getActivityInfo(final String activityName)
            throws NameNotFoundException {
        final String packageName = GenericApplication.getInstance()
                .getApplicationContext().getPackageName();
        final ActivityInfo[] listActivity = GenericApplication.getInstance()
                .getApplicationContext().getPackageManager()
                .getPackageInfo(packageName, PackageManager.GET_ACTIVITIES).activities;
        for (final ActivityInfo activityInfo : listActivity) {
            if (activityInfo.name.toLowerCase(Locale.getDefault()).contains(
                    activityName.toLowerCase(Locale.getDefault()))) {
                return activityInfo;
            }
        }
        return null;
    }

    public PassingValue getPassingValue() {
        return this.mPassingValue;
    }

    public Class<?> getTargetClass() throws NameNotFoundException,
            ClassNotFoundException {
        if (TextUtils.isEmpty(this.mTargetClass)) {
            return null;
        }
        final ActivityInfo activityInfo = getActivityInfo(this.mTargetClass);
        final Class<?> activityClass = Class.forName(activityInfo.name);
        return activityClass;
    }

    public void setPassingValue(final PassingValue passingValue) {
        this.mPassingValue = passingValue;
    }

    public void setTargetClass(final String targetClass) {
        this.mTargetClass = targetClass;
    }
}
